package com.futu.scenicspot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.futu.attraction.domain.City;
import com.futu.attraction.domain.CitySopt;
import com.futu.attraction.mapper.CityMapper;
import com.futu.attraction.mapper.CitySoptMapper;
import com.futu.scenicspot.domain.HScenicImage;
import com.futu.scenicspot.domain.HScenicSpot;
import com.futu.scenicspot.domain.HScenicSpotCategory;
import com.futu.scenicspot.mapper.HScenicImageMapper;
import com.futu.scenicspot.mapper.HScenicSpotMapper;
import com.futu.scenicspot.service.HScenicSpotCategoryService;
import com.futu.scenicspot.mapper.HScenicSpotCategoryMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 86177
* @description 针对表【h_scenic_spot_category(景点与分类关联表)】的数据库操作Service实现
* @createDate 2025-06-10 17:37:11
*/
@Service
public class HScenicSpotCategoryServiceImpl extends ServiceImpl<HScenicSpotCategoryMapper, HScenicSpotCategory>
    implements HScenicSpotCategoryService{
    @Resource
    private HScenicSpotMapper hScenicSpotMapper;
    @Resource
    private CityMapper cityMapper;
    @Resource
    private CitySoptMapper citySoptMapper;
    @Resource
    private HScenicImageMapper hScenicImageMapper;
    
    @Override
    public List<HScenicSpot> findByCategoryId(Long categoryId) {
        System.err.println("categoryId:"+categoryId);
        QueryWrapper<HScenicSpotCategory> wrapper=new QueryWrapper<>();
        wrapper.eq("category_id",categoryId);
        List<HScenicSpotCategory> hScenicSpotCategories = this.baseMapper.selectList(wrapper);
        System.err.println("分类："+hScenicSpotCategories);
        List<Long> list=new ArrayList<>();
        for(HScenicSpotCategory hScenicSpotCategory:hScenicSpotCategories){
            list.add(hScenicSpotCategory.getScenicId());
        }

        System.err.println("list:"+list);

        QueryWrapper<HScenicSpot> wrapper1=new QueryWrapper<>();
        wrapper1.in("id",list);
        final List<HScenicSpot> hScenicSpots = hScenicSpotMapper.selectList(wrapper1);
        System.err.println(hScenicSpots);
        
        // 为每个景点添加图片信息
        for (HScenicSpot spot : hScenicSpots) {
            addScenicImages(spot);
        }
        
        return hScenicSpots;
    }

    @Override
    public List<HScenicSpot> findByCity(String cityName) {
        // 使用QueryWrapper构建查询条件
        QueryWrapper<City> wrapper = new QueryWrapper<>();
        wrapper.eq("city_name", cityName);
        
        // 使用selectList而不是selectOne，获取所有匹配的城市
        List<City> cities = cityMapper.selectList(wrapper);
        System.err.println("查询到的城市列表: " + cities);
        
        // 如果没有找到城市，返回空列表
        if (cities == null || cities.isEmpty()) {
            System.err.println("未找到城市: " + cityName);
            return new ArrayList<>();
        }
        
        // 获取所有匹配城市的ID
        List<Integer> cityIds = cities.stream().map(City::getId).collect(Collectors.toList());
        System.err.println("城市ID列表: " + cityIds);
        
        // 查询这些城市关联的所有景点ID
        QueryWrapper<CitySopt> wrapper1 = new QueryWrapper<>();
        wrapper1.in("city_id", cityIds);
        List<CitySopt> citySopts = citySoptMapper.selectList(wrapper1);
        
        // 如果没有找到关联景点，返回空列表
        if (citySopts == null || citySopts.isEmpty()) {
            System.err.println("未找到城市关联的景点");
            return new ArrayList<>();
        }
        
        // 获取所有景点ID
        List<Long> spotIds = citySopts.stream().map(CitySopt::getSoptId).collect(Collectors.toList());
        System.err.println("景点ID列表: " + spotIds);
        
        // 查询景点信息
        QueryWrapper<HScenicSpot> wrapper2 = new QueryWrapper<>();
        wrapper2.in("id", spotIds);
        List<HScenicSpot> hScenicSpots = hScenicSpotMapper.selectList(wrapper2);
        System.err.println("查询到的景点列表: " + hScenicSpots);

        // 为每个景点添加图片信息
        for (HScenicSpot spot : hScenicSpots) {
            addScenicImages(spot);
        }
        
        return hScenicSpots;
    }
    
    /**
     * 为景点添加图片信息
     * @param spot 景点对象
     */
    private void addScenicImages(HScenicSpot spot) {
        if (spot == null || spot.getId() == null) {
            return;
        }
        
        // 查询景点的图片列表
        List<HScenicImage> images = hScenicImageMapper.findByScenicId(spot.getId());
        spot.setImages(images);
        
        // 提取图片URL列表，方便前端使用
        if (images != null && !images.isEmpty()) {
            List<String> imageUrls = images.stream()
                .map(HScenicImage::getImageUrl)
                .collect(Collectors.toList());
            spot.setImageUrls(imageUrls);
        }
    }
}




